﻿<?php
namespace core;

class Port{
	
	private $sign				=	'c32de88944cfda16c8bb0119fe3e1b56';
	private $batchPlaceOrder	=	'http://10.24.192.69/api/manage/portalCall!batchPlaceOrder';
	private $cancelOrderUrl     =   'http://10.24.192.69/api/manage/portalCall!cancelOrder';
	public $result;
	
	static public $instance;
	static public function getinstance(){
		if(!self::$instance) self::$instance = new self();
		return self::$instance;
	}
	
	
	public function doOrder($productId,$cradNo,$name,$phone,$playDate,$dsOrderCode){
		header("content-type:text/html;charset=utf-8");
		
		$message = "sign=".$this->sign."&productId=".$productId."&cradNo=".$cradNo."&name=".$name."&phone=".$phone."&playDate=".$playDate."&dsOrderCode".$dsOrderCode;
    	
    	$ch = curl_init();
    	curl_setopt($ch, CURLOPT_URL, $this->addPortalOrder);
    	curl_setopt($ch, CURLOPT_TIMEOUT,1);   
    	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    	curl_setopt($ch, CURLOPT_POST, 1);
    	curl_setopt($ch, CURLOPT_POSTFIELDS, $message);
    	curl_exec($ch);
		curl_close($ch);
	}
	
	public function batchDoOrder($productId,$playDate,$data,$counts){
		header("content-type:text/html;charset=utf-8");
		
		$order['sign']=$this->sign;
		$order['productId']=$productId;
		$order['playDate']=$playDate;
		$order['count']=$counts;
		$order['order']=$data;
		$message = "jsonStr=".json_encode($order);
    	
    	$ch = curl_init();
    	curl_setopt($ch, CURLOPT_URL, $this->batchPlaceOrder);
    	curl_setopt($ch, CURLOPT_TIMEOUT,1);   
    	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    	curl_setopt($ch, CURLOPT_POST, 1);
    	curl_setopt($ch, CURLOPT_POSTFIELDS, $message);
    	curl_exec($ch);
		curl_close($ch);
	}
	public function sendAPIOrder($productId,$playDate,$data,$counts,$source){
		header("content-type:text/html;charset=utf-8");
		
		$order['sign']=$this->sign;
		$order['productId']=$productId;
		$order['playDate']=$playDate;
		$order['count']=$counts;
		$order['order']=$data;
		$message = "jsonStr=".json_encode($order);

		$ch = curl_init();
    	curl_setopt($ch, CURLOPT_URL, $this->batchPlaceOrder);
    	curl_setopt($ch, CURLOPT_TIMEOUT,1);   
    	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    	curl_setopt($ch, CURLOPT_POST, 1);
    	curl_setopt($ch, CURLOPT_POSTFIELDS, $message);
    	$data = curl_exec($ch);
    	
    	if(strlen($data)==0){
		    $data = curl_error($ch);
		    $flag = 1;
		    $this->result['code'] = -1;
		    $this->result['description'] = $data;
		}else{
			$dts = json_decode($data,true);
			$this->result = $dts[0];
		}
    	
		curl_close($ch);
        $txt = "发送预订数据:".$message.";\r\n来源:".$source.";添加时间:".date('Y-m-d H:i:s')."\r\nAPI返回结果：".$data."\r\n";
        $this->logs($txt);
    	if(empty($falg)){
			return true;
		}else{
			return false;
		}
    	
	}
	public function sendFreeOrder($productId,$playDate,$endPlayDate,$data,$counts,$source){
		header("content-type:text/html;charset=utf-8");
		
		$order['sign']=$this->sign;
		$order['productId']=$productId;
		$order['playDate']=$playDate;
		$order['endPlayDate'] = $endPlayDate;
		$order['count']=$counts;
		$order['order']=$data;
		$message = "jsonStr=".json_encode($order);
		$ch = curl_init();
    	curl_setopt($ch, CURLOPT_URL, $this->batchPlaceOrder);
    	curl_setopt($ch, CURLOPT_TIMEOUT,1);   
    	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    	curl_setopt($ch, CURLOPT_POST, 1);
    	curl_setopt($ch, CURLOPT_POSTFIELDS, $message);
    	$data = curl_exec($ch);
    	if( strlen($data)==0 ){
		    $data = curl_error($ch);
		    $flag = 1;
		    $this->result['code'] = -1;
		    $this->result['description'] = $data;
		}else{
			$dts = json_decode($data,true);
			$this->result = $dts[0];
		}
		curl_close($ch);
        $txt = "发送免预约数据:".$message.";\r\n来源:".$source."\r\n添加时间:".date('Y-m-d H:i:s')."\r\nAPI返回结果：".$data."\r\n";
		$this->logs($txt);
    	if(empty($falg)){
			return true;
		}else{
			return false;
		}
	}
	public function cancelOrder($orderCode,$productId){
		header("content-type:text/html;charset=utf-8");
		
		$message = "sign=".$this->sign."&orderCode=".$orderCode."&productId=".$productId;
		 
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $this->cancelOrderUrl);
		curl_setopt($ch, CURLOPT_TIMEOUT,1);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_POST, 1);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $message);

		$data = curl_exec($ch);
		if( strlen($data)==0 ){
		    $data = curl_error($ch);
		    $flag = 1;
		    $this->result['code'] = -1;
		    $this->result['description'] = $data;
		}else{
			$dts = json_decode($data,true);
			$this->result = $dts[0];
		}
		curl_close($ch);
        $txt = "取消预订数据:".$message.";\r\n添加时间：".date('Y-m-d H:i:s')."\r\nAPI返回结果：".$data."\r\n";
		$this->logs($txt);
		if(empty($falg)){
			return true;
		}else{
			return false;
		}
	}
	
	public function logs($txt){
		//logs_write($txt,'sendOrderApi');
	}
	
	
}